#include "napi/native_api.h"

#include <map>
#include <set>
#include <list>
#include <deque>
#include <iostream>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <initializer_list>
#include <forward_list>
#include <optional>
#include <bitset>
#include <variant>
#include <atomic>

using namespace std;


class Student
{
public:
    Student(char *name, int age, float score);
    void show();
    
private:
    
    char *m_name;
    int m_age;
    float m_score;
};

Student::Student(char * name, int age, float score)
{
    m_name = name;
    m_age = age;
    m_score = score;
}
void Student::show()
{

    cout<<m_name<<"的年龄是"<<m_age<<",成绩是"<<m_score<<endl;
}


int test_class()
{
    string name("MichaelHsu");
    
    Student stu(name.c_str(), 15, 92.5);
    stu.show();
    Student *ptu = new Student((char*)"李华", 16, 96);
    ptu->show();
    
    return 0;
}


float AddF(float a, float b)
{
    return (a + b);
}

double AddD(double a, double b)
{
    return (a + b); 
}

int AddI(int a, int b)
{
    return (a + b); 
}


void showMap()
{
    map<int, string> mapStudent;
    mapStudent.insert(pair<int, string>(000, "student_zero"));
    mapStudent.insert(map<int, string>::value_type(001, "student_one"));
    mapStudent[123] = "student_first";
    mapStudent[456] = "student_second";
    
    map<int,string>::iterator iter;  //迭代器遍历
    for (iter = mapStudent.begin(); iter != mapStudent.end(); iter++) {
        cout<<iter->first<<" "<<iter->second<<endl;
    }
    return;
}


void VarTest()
{
    std::vector<int> v = {1, 2, 3};
    std::vector<int> v2 = {11, 22, 33};

    std::string singleStr = "this is string";
    std::map<int, std::string> intMap = {{1, "one"}, {2, "two"}};
    std::map<std::string, std::string> strMap = {{"1", "one"}, {"2", "two"}};
    string singleStr2 = "this is string";
    map<int, string> intMap2 = {{1, "one"}, {2, "two"}};
    map<string, string> strMap2 = {{"1", "one"}, {"2", "two"}};
    std::map<int, std::list<int>> maplist1 = {{11, {111, 11111, 111111}}, {22, {222, 2222, 22222}}}


    std::set<int> se = {3, 4, 5};  /////////// no effect
    std::multiset<int> mSet = {4, 4, 5};  /////////// no effect
    std::list<int> li = {7, 8, 9};
    std::queue<int> q;  /////////// no effect
    q.push(11);
    q.push(12);
    std::deque<int> dq = {7, 8, 9}; /////////// no effect
    dq.push_back(13);
    dq.push_back(14);

    std::vector<int> vecArray[2] = {{1, 2, 3}, {4, 5, 6}};
    std::map<int, std::string> mapArray[2];
    mapArray[0].insert({1, "one"});
    mapArray[0].insert({2, "two"});
    mapArray[1].insert({3, "I"});
    mapArray[1].insert({4, "II"});
    std::forward_list<int> forwardList = {11, 22, 33};    /////////// no effect
    //std::optional<double> optionalDouble = 2.0;   // only available from C++17 onwards
    std::bitset<8> b(12);  // 00001100
    //std::variant<Another, Empty> variantObj = another2; // only available from C++17 onwards
    std::atomic<int> atomicInt(7);

    std::unordered_set<int> unSet = {3, 4, 5};   /////////////////////  lldb crash
    std::unordered_map<int, int> unMap = {{1, 100}, {2, 200}}; //////////////////  lldb crash

    string sss = "qweqwe";
    cout << sss << endl;
}


static napi_value Add(napi_env env, napi_callback_info info)
{
    size_t requireArgc = 2;
    size_t argc = 2;
    napi_value args[2] = {nullptr};

    VarTest();
    napi_get_cb_info(env, info, &argc, args , nullptr, nullptr);
   
    test_class();
    
    napi_valuetype valuetype0;
    napi_typeof(env, args[0], &valuetype0);

    napi_valuetype valuetype1;
    napi_typeof(env, args[1], &valuetype1);

    double value0;
    napi_get_value_double(env, args[0], &value0);

    double value1;
    napi_get_value_double(env, args[1], &value1);

    napi_value sum;
    napi_create_double(env, value0 + value1, &sum);

    return sum;

}

EXTERN_C_START
static napi_value Init(napi_env env, napi_value exports)
{
    napi_property_descriptor desc[] = {
        { "add", nullptr, Add, nullptr, nullptr, nullptr, napi_default, nullptr }
    };
    napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
    return exports;
}
EXTERN_C_END

static napi_module demoModule = {
    .nm_version =1,
    .nm_flags = 0,
    .nm_filename = nullptr,
    .nm_register_func = Init,
    .nm_modname = "hello",
    .nm_priv = ((void*)0),
    .reserved = { 0 },
};

extern "C" __attribute__((constructor)) void RegisterHelloModule(void)
{
    napi_module_register(&demoModule);
}
